package com.hzdl.cinema.dao.impl;

import com.hzdl.cinema.dao.PlayViewDao;
import com.hzdl.cinema.domain.PageBean;
import com.hzdl.cinema.domain.Play;
import com.hzdl.cinema.util.JDBCUtils;
import org.junit.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class PlayViewDaoImpl implements PlayViewDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Play> findMovie() {
        //首页展示的影片
        String s = "select * from play";
        List<Play> playList = jdbcTemplate.query(s, new BeanPropertyRowMapper<>(Play.class));
        return playList;
    }

    //热门
    @Override
    public List<Play> findHot() {
        String s = "SELECT * from play where play_Id in " +
                "(select play_Id from schedule where schedule_Id in (select schedule_Id from ticket where user_Id is not null))";

        List<Play> playList = jdbcTemplate.query(s, new BeanPropertyRowMapper<>(Play.class));
        return playList;
    }


    //查询总条数
    @Override
    public int findTotalCount() {
        String s = "select count(*) from play ";
        Integer totalCount = jdbcTemplate.queryForObject(s, Integer.class);
        return totalCount;
    }

    //每页展示的数据
    @Override
    public List<Play> findList(int startRow, int rows) {
        String s = "select * from play limit ?,?";
        List<Play> playList = jdbcTemplate.query(s, new BeanPropertyRowMapper<>(Play.class), startRow, rows);
        return playList;
    }

    @Override
    public Integer searchTotalCount(String searchOne) {
        String s = "select count(*)from play where play_Name like concat('%',?,'%')";
        Integer totalCount = null;
        try {
            totalCount = jdbcTemplate.queryForObject(s, Integer.class, searchOne);
        } catch (DataAccessException e) {
//            e.printStackTrace();
        }
        return totalCount;
    }

    @Override
    public List<Play> findSearch(String searchOne, int startRow, int rows) {
//        搜索查询到的影片
        String s = "select * from play where play_Name like concat('%',?,'%')limit ?,?";
        List<Play> playList = null;
        try {
            playList = jdbcTemplate.query(s, new BeanPropertyRowMapper<>(Play.class), searchOne, startRow, rows);
        } catch (DataAccessException e) {
//            e.printStackTrace();
        }

        return playList;
    }

    //影片详情
    @Override
    public Play filmDetails(String playName) {
        String s = "select * from play where play_Name=?";
        Play play = null;
        try {
            play = jdbcTemplate.queryForObject(s, new BeanPropertyRowMapper<>(Play.class),playName);
        } catch (DataAccessException e) {
//            e.printStackTrace();
        }
        return play;
    }


    @Test
    public void method() {
        String s = "select * from play where play_Name=?";

        Play play = null;
        try {
            play = jdbcTemplate.queryForObject(s, new BeanPropertyRowMapper<>(Play.class),"绿皮书" );
        } catch (DataAccessException e) {
            e.printStackTrace();
        }

        System.out.println(play);
    }
}
